import 'dart:developer' as developer;

import 'package:dio/dio.dart';
import 'package:flutter_common_fragement/common/config/index.dart';
import 'package:flutter_common_fragement/https/helper.dart';

import 'interceptors/token.dart';

enum MethodsType { get, post, put, delete, head, path, download }

final CancelToken _cancelToken = CancelToken();

class HttpManager {
  late Dio _dio;

  Dio get dio => _dio;

  static HttpManager? _instance;

  HttpManager._internal() {
    _dio = Dio(BaseOptions(
      baseUrl: HttpConfig.baseUrl,
      connectTimeout: const Duration(milliseconds: HttpConfig.connectTimeOut),
      sendTimeout: const Duration(milliseconds: HttpConfig.sendTimeOut),
      maxRedirects: HttpConfig.maxRedirects,
      contentType: HttpConfig.jsonContentType,
    ));

    /// 证书校验 原生平台
    dio.httpClientAdapter = NativeHttpAdapter();

    // /// 证书校验 Web平台
    // dio.httpClientAdapter = WebHttpClientAdapter(withCredentials: true);

    /// 转换器
    _dio.transformer = ResponseTransformer();

    /// 拦截器
    _dio.interceptors.add(TokenInterceptors(_cancelToken));
  }

  /// 工厂构建单例
  factory HttpManager() {
    _instance ??= HttpManager._internal();
    return _instance!;
  }

  /// 取消所有的请求
  void cancelAllRequest() {
    _cancelToken.cancel("取消请求");
  }

  // /// 代理
  // void setProxyAddress(String ip) {
  //   (_dio.httpClientAdapter as IOHttpClientAdapter).onHttpClientCreate =
  //       (HttpClient client) {
  //     client.findProxy = (uri) => 'PROXY $ip';
  //     return client;
  //   };
  // }

  /// 获取 https 实例的方法
  Future<Response> request(RequestConfig config) async {
    final Options options = config.options ?? Options();
    options.method = config.method.toUpperCase();

    final response = await _dio.request(
      config.path,
      queryParameters: config.queryParameters,
      data: config.data,
      options: config.options,
      cancelToken: config.cancelToken,
    );

    if (config.showLog) developer.log(response.toString());

    return response;
  }

  // Future<Response> request(
  //   String path, {
  //   MethodsType? method,
  //   Map<String, dynamic>? queryParameters,
  //   dynamic data,
  //   ResponseType? responseType,
  //   Map<String, dynamic>? headers,
  //   ProgressCallback? onSendProgress,
  //   ProgressCallback? onReceiveProgress,
  //   bool showLog = false,
  // }) async {
  //   var options = Options(
  //
  //       /// 默认 GET
  //       method: methods[method?.index] ?? 'GET',
  //       responseType: responseType ?? ResponseType.json);
  //
  //   /// 自定义header
  //   if (headers != null) {
  //     options.headers?.addAll(headers);
  //   }
  //
  //   /// 处理请求
  //   final response = await _dio.request(
  //     path,
  //     queryParameters: queryParameters,
  //     data: data,
  //     options: options,
  //     onSendProgress: onSendProgress,
  //     onReceiveProgress: onReceiveProgress,
  //   );
  //
  //   if (showLog) developer.log(response.toString());
  //
  //   return response;
  // }

  Future<Response> get(RequestConfig config) async {
    final response = await _dio.get(
      config.path,
      queryParameters: config.queryParameters,
      options: config.options,
      cancelToken: config.cancelToken,
    );

    if (config.showLog) developer.log(response.toString());

    return response;
  }

  Future<Response> post(RequestConfig config) async {
    final response = await _dio.post(
      config.path,
      data: config.data,
      onSendProgress: config.onSendProgress,
      onReceiveProgress: config.onReceiveProgress,
    );

    if (config.showLog) developer.log(response.toString());

    return response;
  }
}
